package models

import org.joda.time.DateTime
import play.api.libs.functional.syntax._
import play.api.libs.json._

object JsonFormatters {
  implicit val dateTimeFormatter: Format[DateTime] = new Format[DateTime] {
    override def writes(o: DateTime): JsValue = JsNumber(o.getMillis)

    override def reads(json: JsValue): JsResult[DateTime] = JsSuccess(new DateTime(json.validate[Long].get))
  }

  implicit val userFormatter: Format[User] = Json.format[User]

  implicit val userQueryFormatter: Format[UserQuery] = Json.format[UserQuery]


  implicit val organizationFormatter: Format[Organization] = Json.format[Organization]

  implicit val organizationQueryFormatter: Format[OrganizationQuery] = Json.format[OrganizationQuery]

  implicit val loginFormatter = Json.format[Login]

  implicit val userRoleFormatter = Json.format[UserRole]

  implicit val roleFormatter = Json.format[Role]

  implicit val userRoleRFormatter = Json.format[UserRoleR]

  implicit val profileFormatter = Json.format[Profile]

  implicit val jurisdictionQueryFormatter = Json.format[JurisdictionQuery]

  implicit val jurisdictionFormatter = Json.format[Jurisdiction]

  implicit val dictQueryFormatter = Json.format[DictQuery]

  implicit val dictFormatter = Json.format[Dict]

  implicit val dictItemFormatter = Json.format[DictItem]

  implicit val dictItemQueryFormatter = Json.format[DictItemQuery]

  implicit val roleQueryFormatter = Json.format[RoleQuery]

  implicit val dictWithDictItemsFormatter = Json.format[DictWithDictItem]

  implicit val dictWithDictItemsReturnFormatter = ((__ \ "dict").format[Dict] and (__ \ "dictItems").formatNullable[Seq[DictItem]])

  implicit val sProfileRolesFormatter = Json.format[SProfileRoles]

  implicit val sProfileFormatter = Json.format[SProfile]
}
